
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[GroupTournaments_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[GroupTournaments_Insert]

IF OBJECT_ID(N'[dbo].[GroupTournaments_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[GroupTournaments_Update]

IF OBJECT_ID(N'[dbo].[GroupTournaments_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[GroupTournaments_Delete]

IF OBJECT_ID(N'[dbo].[GroupTournaments_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[GroupTournaments_Select]

--endregion

GO


--region [dbo].[GroupTournaments_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[GroupTournaments_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdGroupTournament int = null,
	@GroupTournamentName nvarchar(1000) = null,
	@IsActived bit = null,
	@SortOrder int = null,
	@Icon nvarchar(100) = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[GroupTournaments]
					WHERE
					(
						(@IdGroupTournament is null OR [GroupTournaments].[IdGroupTournament] = @IdGroupTournament)
						AND (@GroupTournamentName is null OR [GroupTournaments].[GroupTournamentName] like @GroupTournamentName)
						AND (@IsActived is null OR [GroupTournaments].[IsActived] = @IsActived)
						AND (@SortOrder is null OR [GroupTournaments].[SortOrder] = @SortOrder)
						AND (@Icon is null OR [GroupTournaments].[Icon] like @Icon)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[GroupTournaments].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdGroupTournament' and @SortType = 1 THEN [GroupTournaments].[IdGroupTournament] END ASC,
						CASE WHEN @SortBy = 'IdGroupTournament' and @SortType = 0 THEN [GroupTournaments].[IdGroupTournament] END DESC,
						CASE WHEN @SortBy = 'GroupTournamentName' and @SortType = 1 THEN [GroupTournaments].[GroupTournamentName] END ASC,
						CASE WHEN @SortBy = 'GroupTournamentName' and @SortType = 0 THEN [GroupTournaments].[GroupTournamentName] END DESC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 1 THEN [GroupTournaments].[IsActived] END ASC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 0 THEN [GroupTournaments].[IsActived] END DESC,
						CASE WHEN @SortBy = 'SortOrder' and @SortType = 1 THEN [GroupTournaments].[SortOrder] END ASC,
						CASE WHEN @SortBy = 'SortOrder' and @SortType = 0 THEN [GroupTournaments].[SortOrder] END DESC,
						CASE WHEN @SortBy = 'Icon' and @SortType = 1 THEN [GroupTournaments].[Icon] END ASC,
						CASE WHEN @SortBy = 'Icon' and @SortType = 0 THEN [GroupTournaments].[Icon] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[GroupTournaments]
			Where 
			(
				(@IdGroupTournament is null OR [GroupTournaments].[IdGroupTournament] = @IdGroupTournament)
				AND (@GroupTournamentName is null OR [GroupTournaments].[GroupTournamentName] like @GroupTournamentName)
				AND (@IsActived is null OR [GroupTournaments].[IsActived] = @IsActived)
				AND (@SortOrder is null OR [GroupTournaments].[SortOrder] = @SortOrder)
				AND (@Icon is null OR [GroupTournaments].[Icon] like @Icon)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[GroupTournaments_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[GroupTournaments_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[GroupTournaments_Insert]
	@IdGroupTournament int OUTPUT,
	@GroupTournamentName nvarchar(1000),
	@IsActived bit,
	@SortOrder int,
	@Icon nvarchar(100)

AS


INSERT INTO [dbo].[GroupTournaments] 
(
	[GroupTournamentName],
	[IsActived],
	[SortOrder],
	[Icon]
)
VALUES 
(
	@GroupTournamentName,
	@IsActived,
	@SortOrder,
	@Icon
)

SET @IdGroupTournament = SCOPE_IDENTITY()

--end [dbo].[GroupTournaments_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[GroupTournaments_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[GroupTournaments_Update]
	@IdGroupTournament int,
	@GroupTournamentName nvarchar(1000),
	@IsActived bit,
	@SortOrder int,
	@Icon nvarchar(100)
AS


UPDATE [dbo].[GroupTournaments] SET
	[GroupTournamentName] = @GroupTournamentName,
	[IsActived] = @IsActived,
	[SortOrder] = @SortOrder,
	[Icon] = @Icon
WHERE
	[IdGroupTournament] = @IdGroupTournament

--end [dbo].[GroupTournaments_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[GroupTournaments_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[GroupTournaments_Delete]
	@IdGroupTournament int
AS


DELETE FROM [dbo].[GroupTournaments]
WHERE
(
	[IdGroupTournament] = @IdGroupTournament
)

--end [dbo].[GroupTournaments_Delete]
--endregion

GO
--=========================================================================================--

